package com.can.Service;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch.core.*;
import co.elastic.clients.elasticsearch.core.search.Hit;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import com.can.Mapper.GameStoreMapper;
import com.can.domain.po.Game;
import com.can.domain.po.GameDoc;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;


@SpringBootTest
public class estest {
@Autowired
EsService esService;
@Autowired
GameStoreMapper gameStoreMapper;

@Test
    public void test() throws IOException {
    String serverUrl = "http://localhost:9200";
    RestClient restClient = RestClient
    .builder(HttpHost.create(serverUrl))
    .build();

    // 然后用这个低级代理创建一个json转换器
    ElasticsearchTransport transport = new RestClientTransport(
        restClient, new JacksonJsonpMapper());

    // 最后创建客户端
    ElasticsearchClient client = new ElasticsearchClient(transport);

        try {
            // 创建索引请求
//            IndexRequest<Game> request = IndexRequest.of(b -> b
//                .index("games")          // 索引名称
//                .id("3")                 // 文档ID (可选，不指定则自动随机生成)
//                .document(game)          // 要索引的文档
//                    ---------------------------------------

//              SearchResponse<GameDoc> response = client.search(s -> s
//                .index("games")  // 索引名
//                .query(q -> q
//                    .multiMatch(m -> m
//                        .query("联盟")  // 搜索关键词
////                        .fields("gamename", "content")  // 查询的字段
//                    )
//                ),
//            GameDoc.class
//        );
//            System.out.println(response.hits().hits().get(0).source());
              //                    ---------------------------------------
        // 1. 将 List<GameWithTime> 转换为 BulkOperation 列表
//        List<Gamewithtime> games= gameStoreMapper.selectList(null);
//        List<BulkOperation> operations = games.stream()
//            .map(game -> BulkOperation.of(op -> op
//                .index(idx -> idx
//                    .index("games")       // ES 索引名
//                    .document(game)      // 文档内容
//                )
//            ))
//            .collect(Collectors.toList());
//        BulkRequest bulkRequest = BulkRequest.of(b -> b.operations(operations));
//        BulkResponse response = client.bulk(bulkRequest); // 发送请求到ES
//            System.out.println(response);
//            IndexResponse response = client.index(request);
//            System.out.println(response.hits().hits().get(0).source());
//                    ---------------------------------------
//             SearchResponse<Game> response = client.search(s -> s
//                            .index("games")
//                            .query(q -> q
//                                    .term(t -> t
//                                            .field("id")
//                                            .value(v -> v.longValue(3))
//                                    )
//                            ),
//                    Game.class
//            );
             //                    ---------------------------------------
             SearchResponse<Game> response = client.search(s -> s
                .index("games")
                .query(q -> q
                        .term(t -> t
                                .field("id")
                                .value(v -> v.longValue(3))
                        )
                ),
             Game.class
                                );
            System.out.println(client.delete(d -> d.index("games").id(response.hits().hits().get(0).id())));

            // 处理结果
//            System.out.println("查询结果数量:"+response.hits().hits().size());
//            for (Hit<Game> hit : response.hits().hits()) {
//                System.out.println("Found game: " + hit.source());
//            }
            client.close();
    }catch (Exception e){
            System.out.println(e.getMessage());}
}
}
